/**
 * 
 */
package com.jeecms.wicool.action.tradeArea;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.tradearea.OfficeBuilding;
import com.jeecms.wicool.manager.distribution.DistributionUserMng;
import com.jeecms.wicool.manager.tradeArea.OfficeBuildingMng;
import com.jeecms.wicool.manager.tradeArea.TradeAreaMng;

/**
 * @author Administrator
 *
 */
@Controller
public class OfficeBuildingAct {
	private static final Logger log = LoggerFactory.getLogger(TradeAreaAct.class);

	@Autowired
	private OfficeBuildingMng officeBuildingMng;
	
	@Autowired
	private TradeAreaMng tradeAreaMng;
	
	@Autowired
	private DistributionUserMng distributionUserMng;
	
	@RequestMapping("/officebuilding/v_list.do")
	public String list(String name,Integer tradeId,Integer userId, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = officeBuildingMng.getPage(name,null,tradeId,userId,cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		model.addAttribute("tradeId", tradeId);
		model.addAttribute("userId", userId);
		model.addAttribute("tradeList", tradeAreaMng.getAll());
		model.addAttribute("userList", distributionUserMng.getAll());
		return "tradearea/list_officebuilding";
	}
	
	@RequestMapping("/officebuilding/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		OfficeBuilding trade = new OfficeBuilding();
		model.addAttribute("officebuilding", trade);
		model.addAttribute("tradeList", tradeAreaMng.getAll());
		model.addAttribute("userList", distributionUserMng.getAll());
		return "tradearea/add_officebuilding";
	}
	
	@RequestMapping("/officebuilding/o_save.do")
	public String save(OfficeBuilding bean,Integer tradeId,Integer userId, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean.setTrade(tradeAreaMng.findById(tradeId));
		bean.setUser(distributionUserMng.findById(userId));
		bean = officeBuildingMng.save(bean);
		log.info("save OfficeBuilding id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/officebuilding/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		OfficeBuilding trade = officeBuildingMng.findById(id);
		model.addAttribute("officebuilding", trade);
		model.addAttribute("tradeList", tradeAreaMng.getAll());
		model.addAttribute("userList", distributionUserMng.getAll());
		return "tradearea/edit_officebuilding";
	}
	
	@RequestMapping("/officebuilding/o_update.do")
	public String update(OfficeBuilding bean,Integer tradeId,Integer userId,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean.setTrade(tradeAreaMng.findById(tradeId));
		bean.setUser(distributionUserMng.findById(userId));
		bean = officeBuildingMng.update(bean);
		model.addAttribute("message", "global.success");
		log.info("update OfficeBuilding success. id={}", bean.getId());
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/officebuilding/o_delete.do")
	public String delete(Integer[] ids, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		List<OfficeBuilding> beans = officeBuildingMng.deleteByIds(ids);
		for (OfficeBuilding bean : beans) {
			log.info("delete OfficeBuilding id={}", bean.getId());
		}
		return "redirect:v_list.do";
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		OfficeBuilding entity = officeBuildingMng.findById(id);
		if (errors.ifNotExist(entity, OfficeBuilding.class, id)) {
			return true;
		}
		return false;
	}
}
